package co.th.bjc.md.dao.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import co.th.bjc.md.common.Common;
import co.th.bjc.md.dao.CustomerDao;
import co.th.bjc.md.model.Brand;
import co.th.bjc.md.model.Category;
import co.th.bjc.md.model.CustomerData;
import co.th.bjc.md.model.CustomerDetails;
import co.th.bjc.md.model.Department;
import co.th.bjc.md.model.Hospital;
import co.th.bjc.md.model.Model;
import co.th.bjc.md.model.Product;

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {

	@Override
	public List<CustomerData> getCustomerByCriteria(
			String hispitalType, String hospitalDepartment, 
			int bedCapacityMin, int bedCapacityMax,
			int presidentImp, int presidentRp,
			int directorImp, int directorRp,
			int limitFrom, int limitTo) throws Exception{
				
		DetachedCriteria criteria = DetachedCriteria.forClass(CustomerData.class);
		
		if (Common.inNotNull(hispitalType)){
			criteria.createAlias("hospital","hospital");
			criteria.add(Restrictions.eq("hospital.hospitalType", hispitalType));
		}
		if (Common.inNotNull(hospitalDepartment))
			criteria.add(Restrictions.eq("hospitalDepartment.hospitalDepartmentNameTh", hospitalDepartment));
		
		List<CustomerData> list = getHibernateTemplate().findByCriteria(criteria);
		
		return list;
	}

	
	@Override
	public List<Hospital> getHospitalByCriteria(BigDecimal regionID,String hospitalName,String hospitalNameEN)throws Exception{
		
		
		DetachedCriteria criteria = DetachedCriteria.forClass(Hospital.class);
		if (Common.inNotNull(regionID)){
			criteria.createAlias("region","region");
			criteria.add(Restrictions.eq("region.regionId", regionID));
		}
		else{
			criteria.add(Restrictions.eq("region.regionId", BigDecimal.valueOf(1)));
		}
		
		if (Common.inNotNull(hospitalName)){
			criteria.add(Restrictions.like("hospitalNameTH","%"+hospitalName+"%"));		
		}
		else{
			criteria.add(Restrictions.like("hospitalNameTH","%"+""+"%"));		
		}
		
		if (Common.inNotNull(hospitalNameEN)){
			criteria.add(Restrictions.like("hospitalNameEN","%"+hospitalNameEN+"%"));
			
		}
		else{	
			criteria.add(Restrictions.like("hospitalNameEN","%"+""+"%"));
		}
		
		List<Hospital> list = getHibernateTemplate().findByCriteria(criteria);
		return list;
	}

	@Override
	public CustomerData getCustomerByCriterai(BigInteger customer_id)throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(CustomerData.class);
		
		if (Common.inNotNull(customer_id))
			criteria.add(Restrictions.eq("customerCode", BigDecimal.valueOf(customer_id.longValue())));
		
		List<CustomerData> list = getHibernateTemplate().findByCriteria(criteria);
		
		return list.size()>0?list.get(0):null;
	}
	
	@Override
	public List<Department> getAllDepartment()throws Exception {
   	 	DetachedCriteria criteria = DetachedCriteria.forClass(Department.class);
    	return getHibernateTemplate().findByCriteria(criteria);
	}
	
	@Override
	public List<Category> getAllCategory()throws Exception {
   	 	DetachedCriteria criteria = DetachedCriteria.forClass(Category.class);
    	return getHibernateTemplate().findByCriteria(criteria);
	}
	
	@Override
	public List<Brand> getAllBrand()throws Exception {
   	 	DetachedCriteria criteria = DetachedCriteria.forClass(Brand.class);
    	return getHibernateTemplate().findByCriteria(criteria);
	}
	
	@Override
	public List<Product> getListModel(BigDecimal categoryCode,BigDecimal brandCode ) throws Exception{
		DetachedCriteria criteria = DetachedCriteria.forClass(Product.class);
		if (Common.inNotNull(categoryCode)){
			criteria.createAlias("category","category");
			criteria.add(Restrictions.eq("category.categoryCode", categoryCode));
		}
		if (Common.inNotNull(brandCode)){
			criteria.createAlias("brand","brand");
			criteria.add(Restrictions.eq("brand.brandCode", brandCode));
		}
		return getHibernateTemplate().findByCriteria(criteria);
	}


	@Override
	public CustomerDetails getCustomerDetailsByCriteria(BigInteger customer_id) throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(CustomerDetails.class);
		if (Common.inNotNull(customer_id))
			criteria.add(Restrictions.eq("customerDataId", BigDecimal.valueOf(customer_id.longValue())));
		
		List<CustomerDetails> list = getHibernateTemplate().findByCriteria(criteria);
		
		return list.size()>0?list.get(0):null;
	}


	@Override
	public List<CustomerDetails> getCustomerDetailsByCriteria(
			String hispitalType, String hospitalDepartment, int bedCapacityMin,
			int bedCapacityMax, int presidentImp, int presidentRp,
			int directorImp, int directorRp, int limitFrom, int limitTo)
			throws Exception {
		// TODO Auto-generated method stub
		
		DetachedCriteria criteria = DetachedCriteria.forClass(CustomerDetails.class);
		if (Common.inNotNull(hispitalType))
			criteria.add(Restrictions.like("type", "%"+hispitalType+"%"));
		if (Common.inNotNull(hospitalDepartment))
			criteria.add(Restrictions.like("hospitalDepartment", "%"+hospitalDepartment+"%"));
		if (bedCapacityMin > 0)
			criteria.add(Restrictions.eq("bedCapacityMin", BigDecimal.valueOf(bedCapacityMin)));
		if (bedCapacityMax > 0)
			criteria.add(Restrictions.eq("bedCapacityMax", BigDecimal.valueOf(bedCapacityMax)));
		if (presidentImp > 0)
			criteria.add(Restrictions.eq("presidentImpRate", BigDecimal.valueOf(presidentImp)));
		if (presidentRp > 0)
			criteria.add(Restrictions.eq("presidentRpRate", BigDecimal.valueOf(presidentRp)));
		if (directorImp > 0)
			criteria.add(Restrictions.eq("directorImpRate", BigDecimal.valueOf(directorImp)));
		if (directorRp > 0)
			criteria.add(Restrictions.eq("directorRpRate", BigDecimal.valueOf(directorRp)));
//		criteria.add(Restrictions.eq("bedCapacityMin", BigDecimal.valueOf(limitFrom)));
		
		return getHibernateTemplate().findByCriteria(criteria);
	}


	@Override
	public List<CustomerDetails> getCustomerDetailsAll() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(CustomerDetails.class);
		return getHibernateTemplate().findByCriteria(criteria);
	}

}
